<?php

class toMsg
{

    protected $config = array(
        'seKey' => '1a2b3g6as5s7a', // 验证码加密密钥
        'expire' => 1800, // 验证码过期时间（s）
        'length' => 4, // 验证码位数
        'outime' => 3,
        'memberId' => 0
    );

    /**
     * 架构方法 设置参数
     * 
     * @access public
     * @param array $config
     *            配置参数
     */
    public function __construct($config = array())
    {
        import('@.ORG/Sms/Ucpaas');
        if ($_SESSION['memberInfo']['id'] != null) {
            
            $this->config['memberId'] = $_SESSION['memberInfo']['username'];
        } else {
            
            $this->config['memberId'] = $_POST['username'];
        }
        $this->config = array_merge($this->config, $config);
    }

    public function code()
    {
        $key = $this->authcode($this->config['memberId'] . $this->config['seKey']);
        session($key, NULL);
        for ($i = 0; $i < $this->config['length']; $i ++) {
            $num .= rand(0, 9);
        }
        
        $code = $this->authcode($num);
        $session[$key]['verify_code'] = $code;
        $session[$key]['verify_time'] = NOW_TIME;
        session($key, $session);
        // dump(session($key));
        
        return $num;
    }

    /**
     * 检查验证码
     * 直接调用即可
     */
    public function contrast()
    {
        $key = $this->authcode($this->config['memberId'] . $this->config['seKey']);
        $code = session($key);
        
        // dump($code);
        if ($_POST['code'] != null) {
            if ($this->excTime(time(), $code[$key]['verify_time']) < $this->config['outime']) {
                
                // dump(($_POST['code']));
                $data = $this->authcode($_POST['code']);
                if ($data == $code[$key]['verify_code']) {
                    // dump($data);
                    
                    $info['status'] = '0';
                    $info['error'] = '验证码正确';
                    $info['url'] = '';
                    return $info;
                } else {
                    $info['status'] = '1';
                    $info['error'] = '验证码错误';
                    $info['url'] = '';
                    return $info;
                }
            } else {
                $info['status'] = '1';
                $info['error'] = '已超时';
                $info['url'] = '';
                return $info;
            }
        } else {
            $info['status'] = '1';
            $info['error'] = "请输入验证码";
            $ifnf['url'] = '';
            return $info;
        }
    }

    /**
     * 发送验证码
     * $tag 目标为手机号、邮箱号
     * $type 发送的是什么类型的信息 Email tel wx
     * $sql 是哪个数据库
     */
    public function start($tag = NULL, $type, $sql = NULL)
    {
        if ($sql != null) {
            $query = M($sql);
        }
        if ($tag == null) {
            
            $tag = $query->where('site_id = ' . __SITEID__ . ' and username = ' . $this->config['memberId'])->getField($type);
        }
        if ($type == 'Email') {} else {
            
            $info = $this->sandsns($tag);
        }
        return $info;
    }

    public function sandsns($tel)
    {
        $Ucpass = new Ucpaas();
        
        $MSN = $this->code();
        $to = $tel;
        $param = $MSN . ',' . $this->config['outime'];
        $data = $Ucpass->templateSMS($to, $param);
        if ($data == false) {
            $error = $Ucpass->getError();
        } else {
            $info['status'] = '0';
            $info['error'] = '信息发送成功';
            $info['url'] = '';
        }
        if ($error != null) {
            
            if ($error == 105102) {
                $info['status'] = '1';
                $info['error'] = '手机号码错误';
                $info['url'] = '';
            } else {
                $info['status'] = '1';
                $info['error'] = '信息发送失败，请重新发送';
                $info['url'] = '';
            }
        }
        return $info;
    }

    private function authcode($str)
    {
        $key = substr(md5($this->$this->config['seKey']), 5, 8);
        $str = substr(md5($str), 8, 10);
        return md5($key . $str);
    }

    public function excTime($frontTime, $lastTime)
    {
        return floor($frontTime / 60) - floor($lastTime / 60);
    }
}

